package cn.edu.gzhu.workOrderManagement.typeHandles;

import cn.edu.gzhu.workOrderManagement.enumeration.UserAuthorityEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(UserAuthorityEnum.class)
public class UserAuthorityTypeHandle extends BaseTypeHandler<UserAuthorityEnum> {
    /**
     * @param ps
     * @param i
     * @param parameter
     * @param jdbcType
     * @throws SQLException
     */
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, UserAuthorityEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i,parameter.getRole());
    }

    /**
     * Gets the nullable result.
     *
     * @param rs         the rs
     * @param columnName Colunm name, when configuration <code>useColumnLabel</code> is <code>false</code>
     * @return the nullable result
     * @throws SQLException the SQL exception
     */
    @Override
    public UserAuthorityEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return UserAuthorityEnum.value2Object(rs.getString(columnName));
    }

    /**
     * @param rs
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    @Override
    public UserAuthorityEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return UserAuthorityEnum.value2Object(rs.getString(columnIndex));
    }

    /**
     * @param cs
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    @Override
    public UserAuthorityEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return UserAuthorityEnum.value2Object(cs.getString(columnIndex));
    }
}
